package com.ob.web;

import cn.hutool.core.util.StrUtil;
import com.ob.api.limit.RateLimiter;
import com.ob.core.constants.ObConstants;
import com.ob.core.dto.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.*;

/**
 * @author jasmine
 */
@Slf4j
@EnableAsync
@RestController
@EnableScheduling
@SpringBootApplication
public class ObApplication {

    private static Environment env;

    public static void main(String[] args) {
        try {
            SpringApplication.run(ObApplication.class, args);
            successPrint();
        } catch (Exception e) {
            faultPrint();
        }
    }

    @RequestMapping
    @RateLimiter
    public R obHome() {
        return R.ok(String.format("这里是 「Ob Server」 服务器 「v_%s」",env.getProperty("ob-version")));
    }

    @Autowired
    public void setEnv(Environment env) {
        ObApplication.env = env;
    }

    private static void successPrint () {
        log.info(
                "\n┌───────────────────────────────────────────┐"+
                "\n|       OB/Service Starting Success         |"+
                "\n└───────────────────────────────────────────┘"+
                "\nPORT         : {}" +
                "\nENVIRONMENT  : {}" +
                "\nOB_VERSION   : {}" +
                "\nDESCRIPTIONS : {}"
        ,
        env.getProperty("server.port"),
        env.getProperty("spring.profiles.active"),
        env.getProperty("ob-version"),
        StrUtil.equalsIgnoreCase(env.getProperty("spring.profiles.active"), ObConstants.DEV_DB_NAME) ? ObConstants.DEV_INFO : ObConstants.PROD_WARNING);
    }

    private static void faultPrint () {
        log.info(
                "\n┌───────────────────────────────────────────┐"+
                "\n|        OB/Service Starting fault          |"+
                "\n└───────────────────────────────────────────┘"+
                "\nplease contact us" +
                "\nPROJECT : {}"
        ,
        "https://gitee.com/jasminexz/open-blog");
    }
}
